package hauwei.exam416;

import java.util.*;

public class test {
    static class State{
        String station;
        int line;
        List<String> path;
        int cost;
        int transferCount;
        public State(String station,int line,List<String>path,int cost,int transferCount){
            this.cost=cost;
            this.path=path;
            this.line=line;
            this.station=station;
            this.transferCount=transferCount;
        }
    }
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        int N=Integer.parseInt(scanner.nextLine());
        List<List<String>>lines=new ArrayList<>();
        Map<String,List<Integer>> stationTolines=new HashMap<>();
        Set<String>head=new HashSet<>();

        for (int i=0;i<N;i++){
            String[]stations=scanner.nextLine().split(" ");
            lines.add(Arrays.asList(stations));
            head.add(stations[0]);

            for (String station:stations){
                stationTolines.putIfAbsent(station,new ArrayList<>());
                stationTolines.get(station).add(i);
            }
        }

        String[] startEnd= scanner.nextLine().split(" ");
        String start=startEnd[0];
        String end=startEnd[1];

        Queue<State>queue=new LinkedList<>();
        Set<String>visited=new HashSet<>();

        for(int line:stationTolines.getOrDefault(start,new ArrayList<>())){
            ArrayList<String> strings = new ArrayList<>();
            strings.add(start);
            queue.offer(new State(start,line,strings,2,0));
            visited.add(start+","+line);
        }

        List<String> resultPath=null;
        int minCost=Integer.MAX_VALUE;

        while(!queue.isEmpty()){
            State current=queue.poll();

            if(current.station.equals(end)){
                if(current.cost<minCost){
                    minCost=current.cost;
                    resultPath=current.path;
                }
                continue;
            }
            List<String>currentLineStations=lines.get(current.line);
            int idx=currentLineStations.indexOf(current.station);

            if(idx+1<currentLineStations.size()){
                String nextStation=currentLineStations.get(idx+1);
                String key=nextStation+","+current.line;
                if(!visited.contains(key)){
                    visited.add(key);
                    List<String>newPath=new ArrayList<>(current.path);
                    newPath.add(nextStation);
                    queue.offer(
                            new State(nextStation, current.line,
                                    newPath, current.cost, current.transferCount));
                }
            }
            if (idx-1>=0){
                String prevStation=currentLineStations.get(idx-1);
                String key=prevStation+","+current.line;
                if(!visited.contains(key)){
                    visited.add(key);
                    List<String>newPath=new ArrayList<>(current.path);
                    newPath.add(prevStation);
                    queue.offer(new State(prevStation,current.line,newPath,
                            current.cost, current.transferCount));
                }
            }

            for (int otherLine:stationTolines.getOrDefault(current.station,new ArrayList<>())){
                if(otherLine!= current.line){
                    String key= current.station+","+otherLine;
                    if(!visited.contains(key)){
                        visited.add(key);
                        List<String>newPath=new ArrayList<>(current.path);
                        queue.offer(new State(current.station, otherLine,newPath,
                                current.cost+1, current.transferCount+1));
                    }
                }
            }
        }
        if(resultPath!=null){
            System.out.println(head);
            for (int i=0;i<resultPath.size();i++){
                if(head.contains(resultPath.get(i))){
                    System.out.print(resultPath.get(i));
                    System.out.print("-");
                }
            }
            System.out.println(resultPath.get(resultPath.size()-1));
            System.out.println(minCost);
        }else{
            System.out.println("NA");
        }
    }
}
